package com.kaifamiao.ajax;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.logging.Logger;

@WebServlet( "/hello" )
public class HelloServlet extends HttpServlet {

    private Logger logger = Logger.getLogger( this.getClass().getName() );

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        logger.info( "GET" );
        Map<String,String[]> map = request.getParameterMap();
        logger.info( map.toString() );

        // 设置响应头中的 content-type 字段的值
        // response.setHeader( "content-type" , "text/plain; charset=UTF-8" );
        response.setContentType( "text/plain; charset=UTF-8" );

        PrintWriter out = response.getWriter();

        out.println( "{ \"method\": 'get' , \"success\": true , \"message\": '成功' }" );
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        logger.info( "POST" );
        Map<String,String[]> map = request.getParameterMap();
        logger.info( map.toString() );

        response.setContentType( "text/plain; charset=UTF-8" );
        PrintWriter out = response.getWriter();

        out.println( "{ \"method\": 'post' , \"success\": true , \"message\": '成功' }" );
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        logger.info( "PUT" );
        Map<String,String[]> map = request.getParameterMap();
        logger.info( map.toString() );

        response.setContentType( "text/plain; charset=UTF-8" );

        PrintWriter out = response.getWriter();

        out.println( "{ \"method\": 'put' , \"success\": true , \"message\": '成功' }" );
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        logger.info( "DELETE" );
        Map<String,String[]> map = request.getParameterMap();
        logger.info( map.toString() );

        response.setContentType( "text/plain; charset=UTF-8" );

        PrintWriter out = response.getWriter();

        out.println( "{ \"method\": 'delete' , \"success\": true , \"message\": '成功' }" );
    }

    @Override
    protected void doHead(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        logger.info( "HEAD" );
        Map<String,String[]> map = request.getParameterMap();
        logger.info( map.toString() );

        response.setContentType( "text/plain; charset=UTF-8" );

        PrintWriter out = response.getWriter();

        out.println( "{ \"method\": 'delete' , \"success\": true , \"message\": '成功' }" );
    }
}
